﻿using Api.Core.IRepository.User;
using Api.Core.IServices.User;
using Api.Core.Model;
using Api.Core.Model.Role;
using Api.Core.Model.User;
using SqlSugar;
using System.Threading.Tasks;

namespace Api.Core.Services.User
{
    public class UserInfoService : BaseService<UserEntity>, IUserInfoService
    {
        public UserInfoService(IUserRepository userRepository)
        {
            base.BaseDal = userRepository;
        }

        public async Task<PageModel<UserDto>> GetUser(UserDto userDto)
        {
            string where = "";
            if (!string.IsNullOrWhiteSpace(userDto.UserId))
            {
                where += " UserId = '" + userDto.UserId + "' ";
            }
            if (!string.IsNullOrWhiteSpace(userDto.UserName))
            {
                where += " UserName Like '" + userDto.UserName + "%' ";

            }

            var UserDto = await base.QueryMuch<UserEntity, UserRoleEntity, RoleEntity, UserDto>(
                     userDto.pageIndex,
                     userDto.pageSize,
                     null,
                     where,
                    (u, ur, r) => new object[]
                    {
                    JoinType.Left, u.UserId == ur.UserID,
                    JoinType.Left,  ur.RoleId == r.RoleId
                    },

                    (u, ur, r) => new UserDto()
                    {
                        UserId = u.UserId,
                        UserName = u.UserName,
                        RoleName = r.RoleName,
                        IsDel = u.IsDel,
                    }
                  );
            return UserDto;
        }
    }

}
